import { createRouter, createWebHashHistory, RouteRecordRaw } from 'vue-router';
import { userStore } from '../store/user';

const routes: Array<RouteRecordRaw> = [
  {
    path: '/',
    redirect: '/login',
  },
  {
    path: '/login',
    meta: { title: '登录页' },
    component: import('@/pages/Login/index.vue'),
  },
  {
    path: '/login/ServiceAgree',
    meta: { title: '服务协议' },
    component: import('@/pages/Login/ServiceAgree.vue'),
  },
  {
    path: '/login/PrivacyPolicy',
    meta: { title: '隐私政策' },
    component: import('@/pages/Login/PrivacyPolicy.vue'),
  },
  {
    path: '/task',
    meta: { title: '任务主页' },
    component: import('@/pages/Task/index.vue'),
  },
  {
    path: '/task/search',
    meta: { title: '任务搜索' },
    component: import('@/pages/Task/Search.vue'),
  },
  {
    path: '/task/details/:id',
    meta: { title: '任务详情' },
    component: import('@/pages/Task/Details.vue'),
  },
  {
    path: '/task/companySource/:id',
    meta: { title: '公司任务主页' },
    component: import('@/pages/Task/CompanySource.vue'),
  },

  {
    path: '/contract',
    meta: { title: '合约主页' },
    component: import('@/pages/Contract/index.vue'),
  },
  {
    path: '/contract/details/:id',
    meta: { title: '合约详情' },
    component: import('@/pages/Contract/Details.vue'),
  },
  {
    path: '/contract/progress/:id',
    meta: { title: '合约进度' },
    component: import('@/pages/Contract/Progress.vue'),
  },

  {
    path: '/message',
    meta: { title: '消息主页' },
    component: import('@/pages/Message/index.vue'),
  },
  {
    path: '/message/systemList',
    meta: { title: '系统消息列表' },
    component: import('@/pages/Message/SystemList.vue'),
  },
  {
    path: '/message/systemDetails/:id',
    meta: { title: '系统消息详情' },
    component: import('@/pages/Message/SystemDetails.vue'),
  },
  {
    path: '/message/talk/:taskId/:userId',
    meta: { title: '对话消息' },
    component: import('@/pages/Message/Talk.vue'),
  },
  {
    path: '/message/talent/:resumeId/:userId',
    meta: { title: '人才主页' },
    component: import('@/pages/Message/Talent.vue'),
  },
  // {
  //   path: '/message/talent/details/:id',
  //   meta: { title: '人才详情' },
  //   component: import('@/pages/Talent/Details.vue'),
  // },

  {
    path: '/my',
    meta: { title: '我的主页' },
    component: import('@/pages/My/index.vue'),
  },
  {
    path: '/my/user',
    meta: { title: '用户中心' },
    component: import('@/pages/My/User/index.vue'),
  },
  {
    path: '/my/user/authReal',
    meta: { title: '实名认证' },
    component: import('@/pages/My/User/AuthReal.vue'),
  },
  {
    path: '/my/user/certified',
    meta: { title: '已完成实名' },
    component: import('@/pages/My/User/Certified.vue'),
  },
  {
    path: '/my/user/identitySwitch',
    meta: { title: '切换身份' },
    component: import('@/pages/My/User/IdentitySwitch.vue'),
  },
  {
    path: '/my/contract/:id',
    component: () => import('@/pages/My/Contract/index.vue'),
  },
  {
    path: '/my/contract/add',
    component: () => import('@/pages/My/Contract/add.vue'),
  },
  {
    path: '/my/set',
    meta: { title: '我的设置' },
    component: import('@/pages/My/Set/index.vue'),
  },
  {
    path: '/my/feedback',
    meta: { title: '意见反馈' },
    component: import('@/pages/My/Feedback/index.vue'),
  },
  {
    path: '/my/account',
    meta: { title: '我的账户' },
    component: import('@/pages/My/Account/index.vue'),
  },
  {
    path: '/my/account/advance',
    meta: { title: '账户提现' },
    component: import('@/pages/My/Account/Advance.vue'),
  },
  {
    path: '/my/account/coinExplain',
    meta: { title: '无忧币说明' },
    component: import('@/pages/My/Account/CoinExplain.vue'),
  },
  {
    path: '/my/account/depositExplain',
    meta: { title: '押金说明' },
    component: import('@/pages/My/Account/DepositExplain.vue'),
  },

  {
    path: '/my/resume',
    meta: { title: '我的简历' },
    component: import('@/pages/My/Resume/index.vue'),
  },
  {
    path: '/my/resume/preview',
    meta: { title: '简历预览' },
    component: import('@/pages/My/Resume/Preview.vue'),
  },

  {
    path: '/my/collect',
    meta: { title: '我的收藏' },
    component: import('@/pages/My/Collect/index.vue'),
  },
  {
    path: '/my/about',
    meta: { title: '关于我们' },
    component: import('@/pages/My/About/index.vue'),
  },

  {
    path: '/admin/home',
    component: () => import('@/pages/Admin/Home/index.vue'),
  },
  {
    path: '/admin/home/contract',
    component: () => import('@/pages/Admin/Home/Contract/index.vue'),
  },
  {
    path: '/admin/home/auditor',
    component: () => import('@/pages/Admin/Home/Auditor/index.vue'),
  },
  {
    path: '/admin/home/auditor/task/:id',
    component: () => import('@/pages/Admin/Home/Auditor/TaskDetail.vue'),
  },
  {
    path: '/admin/home/auditor/talent/:id',
    component: () => import('@/pages/Admin/Home/Auditor/TalentDetail.vue'),
  },
  {
    path: '/admin/home/auditor/company/:id',
    component: () => import('@/pages/Admin/Home/Auditor/CompanyDetail.vue'),
  },
  {
    path: '/admin/home/pay',
    component: () => import('@/pages/Admin/Home/Pay/index.vue'),
  },
  {
    path: '/admin/home/user',
    component: () => import('@/pages/Admin/Home/User/index.vue'),
  },

  {
    path: '/talent',
    component: () => import('@/pages/Talent/index.vue'),
  },
  {
    path: '/talent/details/:id',
    component: () => import('@/pages/Talent/Details.vue'),
  },
];

const router = createRouter({
  history: createWebHashHistory(),
  routes,
});

router.beforeEach((to, from, next) => {
  const store = userStore();
  if (store.token) {
    // 登录
    if (to.path === '/login') {
      next('/task');
    } else {
      next();
    }
  } else {
    // 未登录
    if (
      to.path === '/login' ||
      to.path === '/login/serviceAgree' ||
      to.path === '/login/privacyPolicy'
    ) {
      next();
    } else {
      next('/login');
    }
  }
});

export default router;
